import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

//主界面
import Home from '@/views/Home/Home.vue'



const routes: Array<RouteRecordRaw> = [
  {
    path:'',
    redirect:'/home'
  },
  {
    path:'/home',
    component:Home,
    children:[
      {
        path:'',
        redirect:'/home/rotation'
      },
      {
        path:'rotation',
        component:() => import("@/views/Home/Rotation/Rotation.vue")
      },
      {
        path:'hot',
        component: () => import("@/views/Home/Hotsale/Hotsale.vue")
      },
      {
        path:'new',
        component: () => import("@/views/Home/Hotsale/Hotsale.vue")
      },
      {
        path:'recommend',
        component: () => import("@/views/Home/Hotsale/Hotsale.vue")
      },
      {
        path:'classification',
        component: () => import("@/views/Home/Classification/Classification.vue")
      },
      {
        path:'goods',
        component: () => import("@/views/Home/Goods/Goods.vue")
      },
      {
        path:'addgoods',
        component: () => import("@/views/Home/addGoods/AddGood.vue")
      },
      {
        path:'vip',
        component: () => import('@/views/Home/vip/Vip.vue')
      },
      {
        path:'modify',
        component: () => import("@/views/Home/modify/Modify.vue")
      }
    ]
  },
  {
    path:'/login',
    component:() => import('@/views/Login/Login.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next) => {
  if(to.path.search(/^\/home*/i) !== -1){
    if(localStorage.getItem('token')){
      next()
      
    }else{
      router.push('/login')
    }
  }

  next()
})

export default router
